(function(win){
	var Z = win.Z, Bundle = Z.Bundle, BundleList = Z.BundleList,
		bundle = new Bundle({'tmpl' : 'server'}),
		form, loader;
		
	bundle.addEvent('a.add', 'click', function(){
		form.clear().open();
		return false;
	});
	
	bundle.addEvent('tbody', 'click', 'a.edit', function(){
		var id = $(this).parent().siblings().first().find(':checkbox').val();
		Z.ajax({
			'url' : Z.route('server','find'),
			'data' : {'id' : id},
			'success' : function(data){
				form.open().fill(data);
			}
		});
		return false;
	});

	bundle.onload = function(){
		form = new Z.Form({
			'title' : '添加/修改',
			'content' : bundle.find('script.form-tmpl').html(),
			'width' : 500,
			'action' : Z.route('server','set')
		});

		form.onsubmit = function() {
			loader.reload();
		};
		
		loader = new Z.Loader({
			'url': Z.route('server', 'records'),
			'listTmpl': bundle.find('script.list-tmpl').html(),
			'list': bundle.find('tbody'),
			'pagination': bundle.find('tfoot td')
		});

		loader.load({'pageSize' : 20});
	};
	
	BundleList.append(bundle);
})(window);